using System;
using System.Windows.Forms;
using FireStarter.Base;
using Firestarter.DB.Schema.UI;
using Firestarter.DB_DomainModel.Logic;
using FireStarterModeller.BO;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Base;
using log4net;

namespace Firestarter.DB.Schema
{
	static class Program
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(Program));

		[STAThread]
		static void Main()
		{
			GlobalRegistry.ApplicationName = "Firestarter_DB_Schema";
			GlobalRegistry.ApplicationVersion = "v1.0";
            
            HabaneroAppWin mainApp = new HabaneroAppWin(GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion);
			mainApp.ClassDefsXml = BOBroker.GetClassDefsXml();
			try
			{
				log.Debug(string.Format("-------- {0} {1} Starting  ---------", GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion));
				if (!mainApp.Startup()) return;
			    BORegistry.DataAccessor = new DataAccessorInMemory();
			}
			catch (Exception ex)
			{
				log.Error(ex.Message);
			}

			try
			{
				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);

				FormWin programForm = new FormWin();
				SetupMainForm(programForm);
				Application.Run(programForm);
			}
			catch (Exception ex)
			{
				GlobalRegistry.UIExceptionNotifier.Notify(ex,
						"An error has occurred in the application.",
						"Application Error");
			}
		}

		public static void SetupMainForm(IFormHabanero programForm)
		{
		    IControlFactory controlFactory = new ControlFactoryWin();
            IMainMenuHabanero mainMenuHabanero = SetupMainMenu(programForm, controlFactory, new MenuBuilderWin(controlFactory));
			mainMenuHabanero.DockInForm(programForm);
			programForm.Text = GlobalRegistry.ApplicationName + " " + GlobalRegistry.ApplicationVersion;
			programForm.WindowState = Habanero.Faces.Base.FormWindowState.Maximized;
		}        

		public static IMainMenuHabanero SetupMainMenu(IFormHabanero programForm, IControlFactory controlFactory, IMenuBuilder menuBuilder)
		{
            HabaneroMenu mainMenu = new HabaneroMenu("Main", programForm, controlFactory);

			HabaneroMenu currentMenu = mainMenu.AddSubMenu("&File");
			HabaneroMenu.Item currentMenuItem = currentMenu.AddMenuItem("E&xit");
			currentMenuItem.CustomHandler += delegate { programForm.Close(); };

			HabaneroMenu formsMenu = mainMenu.AddSubMenu("&Forms");
            formsMenu.AddMenuItem("Treeview").FormControlCreator += (() =>
            {
                DatabaseInfoChangesControl databaseInfoChangesControl = new DatabaseInfoChangesControl();
                databaseInfoChangesControl.Database = ViewTestDbRevEngWizard.CreateTestDatabaseInfo();
                return databaseInfoChangesControl;
            });
            //formsMenu.AddMenuItem("Rev Eng Wizard").CustomHandler += (delegate
            //{
            //    ReverseEngineering reverseEngineering = TestDbSchemaReverseEngineeringWizardControl.CreateTestReverseEngineering();
            //    IDBChangesImporter importer = new DBChangesImporter();
            //    DbSchemaReverseEngineeringWizardControl.ShowDBReverseEngineeringWizard(reverseEngineering, importer);
            //});
            formsMenu.AddMenuItem("Schema Investigator").FormControlCreator += (() => new SchemaInvestigator());

			AddGeneratedFormsMenu(mainMenu, controlFactory);

			return menuBuilder.BuildMainMenu(mainMenu);
		}

		private static void AddGeneratedFormsMenu(HabaneroMenu menu, IControlFactory controlFactory)
		{
			HabaneroMenu dataMenu = menu.AddSubMenu("&Generated Forms");
			foreach (Type type in typeof (Program).Assembly.GetTypes())
			{
				if (!type.IsSubclassOf(typeof (MenuItemCreator))) continue;
				MenuItemCreator menuItemCreator = (MenuItemCreator) Activator.CreateInstance(type, new object[] {});
				menuItemCreator.AddToMenu(dataMenu, controlFactory);
			}
		}
	}
}
